# -*- coding: utf-8 -*-
"""
Created on Sun Dec 26 17:24:03 2021

@author: u5390570
"""

import requests
from bs4 import BeautifulSoup
import csv

login_url = 'https://sus.geacron.com/login/'
data = {
    'username': 'charles.miller@anu.edu.au',
    'password': 'KaiTM2016'
}

with requests.Session() as s:
    response = requests.post(login_url , data)
    index_page= s.get('http://sus.geacron.com/terms/term_event_en.html')
    soup = BeautifulSoup(index_page.text, 'html.parser')
    print(soup.title)
    battles=soup.find_all('a',href=True)
    battles=battles[32:len(battles)-1]
    battle_name=[]
    latitude=[]
    longitude=[]
    year=[]
    chars2go=['x','=','&','amp']
    chars2go2=['y','=','&','amp']
    for b in battles:
        battle_name.append(b["name"])
        bat=str(b)
        tmp=list(b)
        year.append(tmp[1].split(' '))
        info=bat.split(";")
        for i in info:
            if (i[0]=='x'):
                temp=i
                for char in chars2go:
                    temp=temp.replace(char,'')
                longitude.append(temp)
            if (i[0]=='y'):
                temp2=i
                for char in chars2go2:
                    temp2=temp2.replace(char,'')
                latitude.append(temp2)
                
        print(b["name"],temp,temp2)
        
with (open("Geacron Locations.csv","w",encoding="utf-8")) as archive_file:
    wr=csv.writer(archive_file)
    wr.writerow(["Battle","Latitude","Longitude","Year"]) 
    for row in zip(battle_name,latitude,longitude,year):
        wr.writerow(row)
            